Structs in C#

C # struct is a lightweight option for a class. I can do almost the same as the class, but instead of class, it is "expensive" to use the structure. The reason is a bit technical, but to sum, one A new example of class is placed on the heap, where a new instant stroke is placed on the stamp. Apart from this, you are not working with rectangle references, as with sections, instead you're working directly with the stretch instances, this also means that when you pass a function directly on the value Depending on the price, depending on the price. More about this about the chapter of the function parameter.

Therefore, when you want to represent more simple data structures, you should use Stractact, and especially if you know that many of them will grow in a lot. There are many examples in the .NET framework, where Microsoft's classified classes are, Instead of using stroke, example digits, rectangular and color structures

First of all I would like to show you an example of using a structure, and after that we will discuss some limitations of using them instead of classes:


class Program
{
    static void Main(string[] args)
    {
        Car car;

        car = new Car("Blue");
        Console.WriteLine(car.Describe());

        car = new Car("Red");
        Console.WriteLine(car.Describe());

        Console.ReadKey();
    }
}

struct Car
{
    private string color;

    public Car(string color)
    {
        this.color = color;
    }

    public string Describe()
    {
        return "This car is " + Color;
    }

    public string Color
    {
        get { return color; }
        set { color = value; }
    }
}

The alert readers have to see that this is a common example code, as well as changes in the structure of the classes, apart from changes in the structure from one class to another. It shows how two concepts are equal. But at the beginning of this chapter, how can they be apart from remarkable technical details?

First of all, the terrain can not be preliminary, which means that you can not declare a member like this:


private string color = "Blue";

If you declare a constructor, all the fields must be specified before leaving the constructor. A structure comes with the default constructor, but as soon as you choose your definition, you agree to initialize all areas in it. It also means that you can not declare your own consultative manufacturer - all the direct producers must take at least one parameter. In our example above, we actually provided a value for the color field. If we do not do this, the compiler will complain.

A structure can not inherit with other classes or strokes, and classes can not be obtained from the stroke. An object receives an object class, but it is for heritage and varieties. Although they support the interface, which means that you can implement your custom custom interface.